/**
 * @author chuzhixin 1204505056@qq.com （不想保留author可删除）
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，vip文档中已提供路由的基础图标与小清新图标的配置方案，请仔细阅读
 */

import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layouts'
import { publicPath, routerMode } from '@/config'

Vue.use(VueRouter)
export const constantRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    hidden: true,
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/commonPage/index/index'),
        meta: {
          title: '首页',
          icon: 'home',
          affix: true,
        },
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/commonPage/login/index'),
    hidden: true,
  },
  {
    path: '/personalCenter',
    redirect: 'personalCenter',
    component: Layout,
    children: [
      {
        path: '/personalCenter',
        name: 'personalCenter',
        component: () => import('@/views/commonPage/personalCenter/index'),
      },
    ],
    hidden: true,
  },
  {
    path: '/404',
    component: Layout,
    redirect: '/404',
    children: [
      {
        path: '/404',
        name: '404',
        component: () => import('@/views/commonPage/error/404'),
      },
    ],
    hidden: true,
  },
  {
    path: '/401',
    component: Layout,
    redirect: '/401',
    children: [
      {
        path: '/401',
        name: '401',
        component: () => import('@/views/commonPage/error/401'),
      },
    ],
    hidden: true,
  },
]

export const asyncRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/index/index'),
        meta: {
          title: '首页',
          icon: 'home',
          affix: true,
        },
      },
    ],
  },
]

const router = new VueRouter({
  base: publicPath,
  mode: routerMode,
  scrollBehavior: () => ({
    y: 0,
  }),
  routes: constantRoutes,
})

export function resetRouter(router) {
  if (!router) {
    location.reload()
  } else {
    const createRouter = new VueRouter({
      base: publicPath,
      mode: routerMode,
      scrollBehavior: () => ({
        y: 0,
      }),
      routes: constantRoutes,
    })
    // 用初始化的matcher替换当前router的matcher
    router.matcher = createRouter.matcher
  }
}

export default router
